 /*************************************************************************
 *
 *  Copyright (C) 2013 Florian Dang <florian.coin@gmail.com>
 *  geometry.cc
 *
 *  This file is part of NatusVerse.
 *
 *  NatusVerse is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  NatusVerse is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with NatusVerse.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************/

#include "graphics/geometry.h"

#include <cmath>

namespace graphics
{
void Circle::Draw()
{
	t_point min, max;
	float dx, dy, rr;
	int x, y;

	min.x = center_.x - radius_;
	max.x = center_.x + radius_;
	min.y = center_.y - radius_;
	max.y = center_.y + radius_;
	rr = radius_ * radius_;

	for(int i = min.x; i <= max.x; i ++)
	{
		dx = i - center_.x;
		dy = sqrt(rr - dx*dx);
		y = center_.y + dy;
		win_.AddPix(i, y, color_);
		y = center_.y - dy;
		win_.AddPix(i, y, color_);
	}

	for(int j = min.y; j <= max.y; j ++)
	{
		dy = j - center_.y;
		dx = sqrt(rr - dy*dy);
		x = center_.x + dx;
		win_.AddPix(x, j, color_);
		x = center_.x - dx;
		win_.AddPix(x, j, color_);
	}

	if(win_.GetAutoprint())
		win_.PrintScreen();
}


void Circle::DrawFill(t_color color)
{
	t_point min, max;
	float dx, dy, rr;

	min.x = center_.x - radius_;
	max.x = center_.x + radius_;
	min.y = center_.y - radius_;
	max.y = center_.y + radius_;
	rr = radius_ * radius_;

	for(int i = min.x; i <= max.x; i ++)
	{
		dx = i - center_.x;
		for(int j = min.y; j <= max.y; j ++)
		{
			dy = j - center_.y;
			if (dx * dx + dy * dy <= rr)
				win_.AddPix(i, j, color);
		}
	}

	if(win_.GetAutoprint())
		win_.PrintScreen();
}

void Rectangle::Draw()
{
	int xmin = p1_.x;
	int xmax = p2_.x;

	if (p2_.x < p2_.x)
	{
		xmin = p2_.x;
		xmax = p1_.x;
	}

	int ymin=p1_.y;
	int ymax=p2_.y;

	if (p2_.y < p1_.y)
	{
		ymin=p2_.y;
		ymax=p1_.y;
	}

	for (int i = xmin; i <= xmax; i ++)
		win_.AddPix(i, ymin, color_);
	for (int i = xmin; i <= xmax; i ++)
		win_.AddPix(i, ymax, color_);

	for (int j = ymin; j <= ymax; j ++)
		win_.AddPix(xmin, j, color_);

	for (int j = ymin;j <= ymax; j ++)
		win_.AddPix(xmax, j, color_);
}

void Rectangle::DrawFill(t_color color)
{
	int xmin = p1_.x;
	int xmax = p2_.x;

	if (p2_.x < p1_.x)
	{
		xmin = p2_.x;
		xmax = p1_.x;
	}

	int ymin=p1_.y;
	int ymax=p2_.y;

	if (p2_.y < p1_.y)
	{
		ymin=p2_.y;
		ymax=p1_.y;
	}

	for (int i = xmin; i <= xmax; i ++)
		for (int j = ymin; j <= ymax; j ++)
			win_.AddPix(i, j, color);
}
} // end namespace
